//
//  RootViewController.m
//  Tang
//
//  Created by 352324497@qq.com on 15/12/3.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "RootViewController.h"

#import "HomeViewController.h"
//#import "UserSetViewController.h"
#import "VideoViewController.h"
#import "UserViewController.h"
#import "LoginViewController.h"

#import "CategorySearchController.h"
@interface RootViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UISegmentedControl *segment;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIView *bottomView;

@end

@implementation RootViewController
{
    CGFloat segmentHeight;
    BOOL _isLogin;
}

- (void)initChildController
{
    CGFloat height = KHeight - segmentHeight - 64;
    HomeViewController *one = [[HomeViewController alloc]init];
    one.view.frame = CGRectMake(0, segmentHeight, KWidth, height);
    [self.scrollView addSubview:one.view];
    [self addChildViewController:one];
    
    VideoViewController *two = [[VideoViewController alloc]init];
    two.view.frame = CGRectMake(KWidth, segmentHeight, KWidth, height);
    [self.scrollView addSubview:two.view];
    [self addChildViewController:two];
    
//    UserSetViewController *three = [[UserSetViewController alloc]init];
//    three.view.frame = CGRectMake(KWidth * 2, segmentHeight, KWidth, height);
//    [self.scrollView addSubview:three.view];
//    [self addChildViewController:three];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 25, 25);
    [button addTarget:self action:@selector(searchButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarbutton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftBarbutton;
    
    // 创建 label 显示
    UILabel *setLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 25)];
    setLabel.text = @"Tang";
    setLabel.textColor = [UIColor orangeColor];
    // 设置 label 字体
    setLabel.font = [UIFont fontWithName:@"SnellRoundhand-Black" size:16];
    // 居左显示
    setLabel.textAlignment = NSTextAlignmentLeft;
    UIBarButtonItem *labelBar = [[UIBarButtonItem alloc]initWithCustomView:setLabel];
    self.navigationItem.leftBarButtonItems = @[leftBarbutton, labelBar];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setImage:[UIImage imageNamed:@"my"] forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 0, 25, 25);
    [button1 addTarget:self action:@selector(myButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithCustomView:button1];
    self.navigationItem.rightBarButtonItem = right;
    
//    self.navigationController.navigationBar.translucent = NO;
    segmentHeight = 40 * KWidth / 414;
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
    self.scrollView.contentSize = CGSizeMake(KWidth * 2, 0);
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    [self initChildController];
    
    NSArray *array = @[@"主页", @"视频"];
    self.segment = [[UISegmentedControl alloc]initWithItems:array];
    self.segment.frame = CGRectMake(0, 0, KWidth, segmentHeight);
    self.segment.momentary = NO;
    self.segment.selectedSegmentIndex = 0;
    [self.segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    self.segment.tintColor = [UIColor clearColor];
    
    [self.segment setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    
    [self.segment setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
    
    self.bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, segmentHeight - 2, KWidth / 2, 2)];
    self.bottomView.backgroundColor = [UIColor orangeColor];
    [self.segment addSubview:self.bottomView];
    [self.view addSubview:self.segment];


}


- (void)segmentAction:(UISegmentedControl *)segment
{
    NSInteger i = segment.selectedSegmentIndex;
    [self.scrollView setContentOffset:CGPointMake(KWidth * i, 0) animated:YES];
    self.bottomView.frame = CGRectMake(KWidth / 2 * i, segmentHeight - 2, KWidth / 2, 2);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    self.segment.selectedSegmentIndex = point.x / KWidth;
    self.bottomView.frame = CGRectMake(KWidth / 2 * self.segment.selectedSegmentIndex, segmentHeight - 2, KWidth / 2, 2);
}

-(void)searchButtonAction
{
    CategorySearchController *categorySearchVC = [[CategorySearchController alloc]init];
    [self.navigationController pushViewController:categorySearchVC animated:NO];
    
 
}

- (void)myButtonAction
{
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser)
    {
        UserViewController *userView = [[UserViewController alloc]init];
        [self.navigationController pushViewController:userView animated:NO];
    }
    else
    {
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"LoginAndRegist" bundle:nil];
        LoginViewController *loginViewVc = [storyBoard instantiateInitialViewController];
        [self presentViewController:loginViewVc animated:NO completion:nil];
//        [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"Login"];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
